<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Upload
 *
 */
class Upload {

	private $files = null;
	private $lastFile = null;
	private static $_instance;
	private $uploadFolder = 'upload/';

	public function checkUpload() {
		if ($_FILES["file"]["error"] > 0) {
			//echo "Error: " . $_FILES["file"]["error"] . "<br />";
			return false;
		} else {
			/* echo "Upload: " . $_FILES["file"]["name"] . "<br />";
			 echo "Type: " . $_FILES["file"]["type"] . "<br />";
			 echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
			 echo "Stored in: " . $_FILES["file"]["tmp_name"]; */
			return true;
		}
	}

	public function saveUpload() {
		global $user;
		if ((
		($_FILES["file"]["type"] == "application/vnd.ms-excel")
		|| ($_FILES["file"]["type"] == "application/vnd.ms-excel")
		)
		&& ($_FILES["file"]["size"] < 20000)) {
			if ($_FILES["file"]["error"] > 0) {
				echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
			} else {
				/* echo "Upload: " . $_FILES["file"]["name"] . "<br />";
				 echo "Type: " . $_FILES["file"]["type"] . "<br />";
				 echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
				 echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; */
				/* if (file_exists("upload/" . $_FILES["file"]["name"])) {
				 echo $_FILES["file"]["name"] . " already exists. ";
				 } else {
				 move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $user->getempresa() . $_FILES["file"]["name"]);
				 echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
				 } */
				$this->lastFile = $this->uploadFolder . $user->getempresa() . $_FILES["file"]["name"];
				move_uploaded_file($_FILES["file"]["tmp_name"], $this->lastFile);
				//echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
			}
		} else {
			echo "Invalid file";
		}
	}

	public function validaFicheiro() {
		global $mensagem;
		$file=file_get_contents($this->lastFile);
		if ($file === false) {
			$mensagem->setMensagem('File Structure with error');
			return false;
		}
		$file = explode(";", $file);
		//printArray($file);
		Test::assertDate($file[0]);

		Test::assertInt((int)$file[1]);
		Test::assertPositive((int)$file[1]);

		Test::assertInt((int)$file[2]);
		Test::assertPositive((int)$file[2]);

		Test::assertCoin((int)$file[3]);



	}

	public static function getInstance() {
		if (!(self::$_instance instanceof self)) {
			self::$_instance = new self();
		}

		return self::$_instance;
	}

	// Do not allow an explicit call of the constructor: $v = new Singleton();
	final private function __construct() {

	}

	// Do not allow the clone operation: $x = clone $v;
	final private function __clone() {

	}

	public function getFiles() {
		return $this->files;
	}

	public function putFiles($var) {
		array_push($this->files, $var);
	}

}

?>
